#!/bin/sh
cd /app/smarttrading-new/smarttrading/app
export ENV_FILE_PATH=/app/smarttrading-new/smarttrading/app/envs/dev.properties
start() {
	#判断进程是否存在，如果不存在就启动它
	PIDS=`ps -ef |grep 'manage:app' |grep -v grep | awk '{print $2}'`
	if [ "$PIDS" != "" ]; then
		echo "Server Already Start ^_^"
	else
		#启动命令
		nohup gunicorn -c gunicorn.conf manage:app &
		# $? 为上条命令执行结果，成功执行则返回0
		if [[ "$?" -eq 0 ]];
		then
			echo "Server Start Success ^_^"
		else
			echo "Server Start failure, please check o(╥﹏╥)o"
		fi
	fi
}

stop() {
	#判断进程是否存在，如果不存在就启动它
	PIDS=`ps -ef |grep 'manage:app' |grep -v grep | awk '{print $2}'`
	if [ "$PIDS" != "" ]; then
		#关闭命令
		ps auxww|grep "manage:app"|grep -v grep|awk '{print $2}'|xargs kill -9
		# $? 为上条命令执行结果，成功执行则返回0
		if [[ "$?" -eq 0 ]];
		then
			echo "Server Stop Success ^_^"
		else
			echo "Server Stop failure, please check o(╥﹏╥)o"
		fi
	else
		echo "Server Already Start ^_^"
	fi
}


restart() {
	#判断进程是否存在，如果不存在就启动它
	PIDS=`ps -ef |grep 'manage:app' |grep -v grep | awk '{print $2}'`
	if [ "$PIDS" != "" ]; then
		echo "Waiting... ^_^"
		#重启命令
		stop
		start
	else
		echo "Server Not Running ^_^"
		#执行启动
		start
	fi
}

case "$1" in
'start')
	start
;;

'stop')
	stop
;;

'restart')
	restart
;;

*)
	echo "Usage: $0 { start | stop | status }"
	exit 1
;;
esac

exit


